<template>
	<view class="container">
		<me-tabs v-model="tabIndex" :tabs="tabs" :fixed="true" :tab-width="130"></me-tabs>
		<swiper :style="{height:height}" :current="tabIndex" @change="swiperChange">
			<swiper-item v-for="(tab,i) in tabs" :key="i">
				<scroll-view scroll-y="true" :style="{height:height}">
					<view class="news-list">
						<view v-for="(item, index) in newsList" :key="index" class="news-item"
							@click="navigateToDetail(item)">
							<image :src="item.pic" class="news-image"></image>
							<view class="news-top">
								<view class="news-text">{{item.title}}</view>
								<view class="news-boot">
									<view class="news-src">{{item.src}}</view>
									<view class="news-time">{{item.time}}</view>
								</view>
							</view>
						</view>
						<view v-if="newsList.length === 0" class="empty-tip">
							<image src="/static/empty.png" mode="aspectFit" class="empty-img"></image>
							<text class="empty-text">暂无新闻数据</text>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	const db = uniCloud.database();

	export default {
		data() {
			return {
				title: 'Hello',
				tabIndex: 0,
				tabs: [{
					name: '最新'
				}, {
					name: '热门'
				}],
				height: 'calc(100vh - 80px)',
				newsList: [],
				loading: false,
				pageSize: 10,
				currentPage: 1,
				hasMore: true
			}
		},
		onLoad() {
			this.getNewsData();
		},
		onReachBottom() {
			if (this.hasMore && !this.loading) {
				this.loadMore();
			}
		},
		methods: {
			async getNewsData() {
				this.loading = true;
				try {
					const res = await db.collection('news')
						.orderBy('time', 'desc')
						.limit(this.pageSize)
						.get();

					this.newsList = res.result.data;
					this.hasMore = res.result.data.length >= this.pageSize;
				} catch (e) {
					console.error('获取新闻数据失败:', e);
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},

			async loadMore() {
				if (!this.hasMore) return;

				this.loading = true;
				try {
					const res = await db.collection('news')
						.orderBy('time', 'desc')
						.skip((this.currentPage) * this.pageSize)
						.limit(this.pageSize)
						.get();

					if (res.result.data.length > 0) {
						this.newsList = [...this.newsList, ...res.result.data];
						this.currentPage++;
						this.hasMore = res.result.data.length >= this.pageSize;
					} else {
						this.hasMore = false;
					}
				} catch (e) {
					console.error('加载更多失败:', e);
				} finally {
					this.loading = false;
				}
			},

			swiperChange(e) {
				this.tabIndex = e.detail.current;
			},
			navigateToDetail(item) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${item._id}&title=${encodeURIComponent(item.title)}`
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		background-color: #f8f8f8;
		padding: 0;
	}

	.news-list {
		padding: 20rpx;
	}

	.news-item {
		display: flex;
		align-items: flex-start;
		padding: 25rpx;
		border-bottom: 1px solid #eee;
		background-color: #fff;
		border-radius: 10rpx;
		/* 添加圆角 */
		margin-bottom: 20rpx;
		/* 添加底部间距 */
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
		/* 添加阴影 */
	}

	.news-image {
		width: 220rpx;
		/* 增大图片宽度 */
		height: 160rpx;
		/* 增大图片高度 */
		border-radius: 8rpx;
		flex-shrink: 0;
		object-fit: cover;
		/* 确保图片填充 */
	}

	.news-top {
		flex: 1;
		margin-left: 30rpx;
		/* 增大文字与图片的间距 */
		display: flex;
		flex-direction: column;
		min-width: 0;
	}

	.news-text {
		font-size: 32rpx;
		/* 增大标题字体大小 */
		line-height: 1.4;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		color: #333;
		margin-bottom: 10rpx;
		/* 添加标题底部间距 */
	}

	.news-boot {
		display: flex;
		align-items: center;
	}

	.news-src {
		font-size: 24rpx;
		color: #999;
		flex: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.news-time {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
		flex-shrink: 0;
	}

	.empty-tip {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;

		.empty-img {
			width: 300rpx;
			height: 300rpx;
			opacity: 0.6;
			margin-bottom: 30rpx;
		}

		.empty-text {
			font-size: 28rpx;
			color: #999;
		}
	}

	.load-more {
		text-align: center;
		padding: 30rpx 0;
		color: #999;
		font-size: 26rpx;
	}
</style>